package org.example.ddm;

import com.android.ddmlib.AndroidDebugBridge;
import com.android.ddmlib.IDevice;
import com.android.ddmlib.SyncService;

public class AdbAccess {
    public static void main(String[] args) {
        AndroidDebugBridge.init(false);
        AndroidDebugBridge bridge = AndroidDebugBridge.createBridge("adb", false);

        waitForDevice(bridge);

        IDevice[] devices = bridge.getDevices();
        if (devices.length == 0) {
            System.out.println("No devices connected.");
            return;
        }

        IDevice device = devices[0];
        pullFile(device, "/sdcard/Download/example.txt", "local_example.txt");

        AndroidDebugBridge.terminate();
    }

    private static void waitForDevice(AndroidDebugBridge bridge) {
        int attempts = 0;
        while (!bridge.hasInitialDeviceList() && attempts < 10) {
            try {
                Thread.sleep(500);
                attempts++;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
//
//    private static void listFilesOnDevice(IDevice device, String remotePath) {
//        try {
//            SyncService sync = device.getSyncService();
//            SyncService.SyncResult result = sync.pull(new String[] {remotePath}, new File("."));
//
//            if (result.getCode() == SyncService.RESULT_OK) {
//                System.out.println("Files listed successfully.");
//            } else {
//                System.out.println("Failed to list files: " + result.getMessage());
//            }
//
//            sync.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    //从设备拉取文件
    private static void pullFile(IDevice device, String remotePath, String localPath) {
        try {
            SyncService sync = device.getSyncService();
            sync.pullFile(remotePath, localPath, new SyncService.ISyncProgressMonitor() {
                @Override public void start(int totalWork) {}
                @Override public void stop() {}
                @Override public boolean isCanceled() { return false; }
                @Override public void startSubTask(String name) {}
                @Override public void advance(int work) {}
            });
            sync.close();
            System.out.println("File pulled successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //推送文件到设备
    public static void pushFile(IDevice device, String localPath, String remotePath) {
        try {
            SyncService sync = device.getSyncService();
            sync.pushFile(localPath, remotePath, new SyncService.ISyncProgressMonitor() {
                @Override public void start(int totalWork) {}
                @Override public void stop() {}
                @Override public boolean isCanceled() { return false; }
                @Override public void startSubTask(String name) {}
                @Override public void advance(int work) {}
            });

            sync.close();
            System.out.println("File pushed successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
